#include "InputTag.h"
#include <iostream>
using namespace std;

InputTag :: InputTag(QWidget* parent, int _code, QString* text) : QTextEdit(parent), Tag(_code), numRow(1), fontSize(18) {
	QFont font("Helvetica");
	font.setPixelSize(fontSize);
	setFont(font);
	setFontPointSize(fontSize);
	setHtml(*text);
	setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
	setAutoFillBackground(true);
	QPalette p;
	p.setColor(QPalette::Base, QColor(255, 239, 135));
	setPalette(p);
	setFrameStyle(QFrame::Plain|QFrame::Panel);
	setBackgroundRole(QPalette::Base);
	document()->adjustSize();
	document()->setTextWidth(-1);
	setMinimumSize(300,  document()->documentLayout()->documentSize().height()+5);
	setMaximumSize(2000, document()->documentLayout()->documentSize().height()+5);
	hlt = new LSyntaxHighlighter(document());
}

void InputTag :: keyPressEvent(QKeyEvent* event) {
	if (event->key() == Qt::Key_Enter || event->key() == Qt::SHIFT + Qt::Key_Return) {
		emit evaluateText(toPlainText(), code);
	}
	else {
		if (event->key() == Qt::Key_Return) {
			document()->adjustSize();
			document()->setTextWidth(-1);
			setMinimumSize(300, document()->documentLayout()->documentSize().height()+document()->documentLayout()->documentSize().height()/numRow + 5);
			setMaximumSize(2000, document()->documentLayout()->documentSize().height()+document()->documentLayout()->documentSize().height()/numRow + 5);
		}
		if (numRow != toPlainText().count('\n')+1) {
			numRow = toPlainText().count('\n')+1;
			setMinimumSize(300, document()->documentLayout()->documentSize().height()+5);
			setMaximumSize(2000, document()->documentLayout()->documentSize().height()+5);
		}
		QTextEdit::keyPressEvent(event);
	}
}

int InputTag :: height() {
	return document()->documentLayout()->documentSize().height();
}

void InputTag :: setFontSize(int size) {
	fontSize = size;
	QFont font("Helvetica");
	font.setPointSize(fontSize);
	setCurrentFont(font);
}


